import xlsx from "xlsx"

export const readExcel = (ev, file, callBack) => {
  let arr = []
  const fileReader = new FileReader();
  fileReader.onload = (ev) => {
    try {
      const data = ev.target.result;
      const XLSX = xlsx;
      const workbook = XLSX.read(data, {
        type: "binary"
      });
      const wsname = workbook.SheetNames[0]; //取第一张表，wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
      console.log('wsname: ', wsname)
      arr = XLSX.utils.sheet_to_json(workbook.Sheets[wsname], {range: 1}); //生成json表格内容，wb.Sheets[Sheet名]获取第一个Sheet的数据
      console.log('读取xlsx: ', arr)
      arr.forEach(a => {
        for (let i in a) {
          a[i] = a[i] + ''
        }
      })
      console.log('读取xlsx转: ', arr)
      callBack(arr)
    } catch (e) {
      return console.log("读取失败!")
    }
  };
  fileReader.readAsBinaryString(file)
}

